package com.yiyang.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yiyang.entity.Building;
import com.yiyang.entity.ResultVO;
import com.yiyang.mapper.BuildingMapper;
import com.yiyang.service.BuildingService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;
@Service
public class BuildingServiceImpl implements BuildingService {
    @Resource
    private BuildingMapper buildingMapper;




    @Override
    public ResultVO<List<Building>> getBuilding(Integer currentPage, Integer pageSize, String name, String numberOfFloor) {
        //设置分页参数
        Page<Building>page = new Page<>(currentPage,pageSize);
        //设置条件查询参数
        QueryWrapper<Building> wrapper=new QueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(name),"name",name)
                .eq(StringUtils.isNotBlank(numberOfFloor),"numberOfFloor",numberOfFloor);
        this.buildingMapper.selectPage(page,wrapper);
        List<Building> list = page.getRecords();


        if(list.isEmpty()){
            return ResultVO.fall();
        }else {
            return ResultVO.success(list,page.getTotal());
        }
    }
    public ResultVO<Void>deleteItem(String id){
        int row=this.buildingMapper.deleteById(id);
        if(row==1){
            return ResultVO.success(null);
        }else {
            return ResultVO.fall();
        }
    }

    @Override
    public ResultVO<Void> addBuilding(Building building) {
        building.setCreatTime(new Date());
        building.setIsDelete(0);
        int insert=this.buildingMapper.insert(building);
        if(insert==1){
            return ResultVO.success(null);
        }else {
            return ResultVO.fall();
        }
    }

    @Override
    public ResultVO<Void> updateBuilding(Building building) {
       int update = this.buildingMapper.updateById(building);
        if(update==1){
            return ResultVO.success(null);
        }else {
            return ResultVO.fall();
        }
    }
}
